package com.red.wood.module.cdn.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.red.wood.common.domain.Page;
import com.red.wood.common.util.CommonUtils;
import com.red.wood.module.cdn.dao.CdnNodeInfoDao;

@Repository
public class CdnNodeInfoDaoImpl implements CdnNodeInfoDao {

	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public Map<String, Object> getPageData(Map<String, Object> param) {
		String count = "0";
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		
		count = listCount(param);
		list = listPageData(param);
		
		Map<String, Object> rs = new HashMap<String, Object>();
		rs.put("totalCount", count);
		rs.put("pageData", list);
		return rs;
	}
	
	public List<Map<String, Object>> listPageData(Map<String, Object> param){
		Page page = (Page) param.get("page");
		int offset = (page.getPageNo() - 1) * page.getPageSize();
		String linkCdnNetId = CommonUtils.valueOf(param.get("linkCdnNetId"));
		StringBuffer sql = new StringBuffer();
		sql.append("select nodeId,nodeName");
		sql.append(" from t_cdn_cdnNetInfo_node node where 1 = 1");
		if(StringUtils.isNotBlank(linkCdnNetId)) {//表示是子网页面下钻过来
			sql.append(getQueryConf(param));
			sql.append(" group by node.cdnId, node.cdnNetId, node.nodeId, node.nodeName");
		}else {
			sql.append(getQueryConf(param));
			sql.append(" group by node.cdnId,node.nodeId, node.nodeName");
		}
		sql.append(" limit " + page.getPageSize() + " offset " + offset+ " ;");
		return jdbcTemplate.queryForList(sql.toString());
	}

	public String listCount(Map<String, Object> param) {
		String linkCdnNetId = CommonUtils.valueOf(param.get("linkCdnNetId"));
		StringBuffer sql = new StringBuffer();
		sql.append("select count(*) count from (");
		sql.append("select nodeId,nodeName");
		sql.append(" from t_cdn_cdnNetInfo_node node where 1 = 1");
		if(StringUtils.isNotBlank(linkCdnNetId)) {//表示是子网页面下钻过来
			sql.append(getQueryConf(param));
			sql.append(" group by node.cdnId, node.cdnNetId, node.nodeId, node.nodeName");
		}else {
			sql.append(getQueryConf(param));
			sql.append(" group by node.cdnId,node.nodeId, node.nodeName");
		}
		sql.append(" ) t");
		Map<String, Object> map = jdbcTemplate.queryForList(sql.toString()).get(0);
		String count = map.get("count").toString();
		return count;
	}
	
	public StringBuffer getQueryConf(Map<String,Object> param) {
		String linkCdnNetId = CommonUtils.valueOf(param.get("linkCdnNetId"));
		String linkCdnId = CommonUtils.valueOf(param.get("linkCdnId"));
		StringBuffer queryConf = new StringBuffer();
		if(StringUtils.isNotBlank(linkCdnNetId)) {
			queryConf.append(" and node.cdnNetId='"+linkCdnNetId+"'");
		}
		if(StringUtils.isNotBlank(linkCdnId)) {
			queryConf.append(" and node.cdnId='"+linkCdnId+"'");
		}
		return queryConf;
	}
}
